package com.example.shangjinlieren.slice;

import com.example.mask.Class.CustomLoading;
import com.example.shangjinlieren.ResourceTable;
import com.example.shangjinlieren.theme.MyColors;
import com.example.shangjinlieren.utils.RpxUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DeviceConfigInfo;
import ohos.aafwk.ability.SystemMemoryInfo;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.PdfGenerator;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.Window;
import ohos.app.Context;
import ohos.app.IAbilityManager;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.global.i18n.phonenumbers.PhoneNumberAttribution;
import ohos.media.image.common.PropertyKey;
import ohos.multimodalinput.event.KeyEvent;
import ohos.net.IpPrefix;
import ohos.net.RouteInfo;
import ohos.sysappcomponents.contact.entity.PhoneNumber;
import ohos.sysappcomponents.settings.SystemSettings;
import ohos.telephony.TelephoneNumberUtils;
import ohos.telephony.TelephonyConstants;

import java.net.InetAddress;
import java.util.Optional;
import java.util.stream.IntStream;

import static ohos.agp.render.PdfGenerator.*;

public class LoginAbilitySlice extends AbilitySlice {
    private Boolean timeDown = true;//倒计时
    private final int timenum=60;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_login);
//        CustomLoading customLoading = null;
//        customLoading = new CustomLoading(getContext());
//        customLoading.show();
        initPage();
    }
    public void initPage(){
        Text Id_text_app_name =(Text) findComponentById(ResourceTable.Id_text_app_name);
        Id_text_app_name.setTextSize((int) RpxUtil.rpx(80,getContext()));
        Id_text_app_name.setMarginTop((int) RpxUtil.rpx(200,getContext()));
        initForm();//表单
        oneKeyLogin();//一键登录
    }
    public void initForm(){
        DirectionalLayout Id_ability_login_form_item =(DirectionalLayout) findComponentById(ResourceTable.Id_ability_login_form_item);
        ShapeElement Id_ability_login_form_itemshapeElement = new ShapeElement();
        /**
         * 盒子
         */
        //默认公共样式
        ShapeElement Id_ability_login_form_item_form_Element = new ShapeElement();
        Id_ability_login_form_item_form_Element.setCornerRadius((int) RpxUtil.rpx(40,getContext()));
        Id_ability_login_form_item_form_Element.setRgbColor(new RgbColor(255,255,255));

        Id_ability_login_form_item.setMarginTop((int) RpxUtil.rpx(120,getContext()));
        Id_ability_login_form_item.setBackground(Id_ability_login_form_itemshapeElement);
        //表单
        //手机号
        Text Id_ability_login_form_item_form_phone_label =(Text) findComponentById(ResourceTable.Id_ability_login_form_item_form_phone_label);
        Id_ability_login_form_item_form_phone_label.setTextSize((int)RpxUtil.rpx(28,getContext()));
            //输入框
        TextField Id_ability_login_form_item_form_phone =(TextField) findComponentById(ResourceTable.Id_ability_login_form_item_form_phone);
        Id_ability_login_form_item_form_phone.setTextSize((int)RpxUtil.rpx(28,getContext()));

        Id_ability_login_form_item_form_phone.setBackground(Id_ability_login_form_item_form_Element);
        Id_ability_login_form_item_form_phone.setMarginLeft((int)RpxUtil.rpx(30,getContext()));
        Id_ability_login_form_item_form_phone.setPaddingLeft((int)RpxUtil.rpx(30,getContext()));
        Id_ability_login_form_item_form_phone.setPaddingRight((int)RpxUtil.rpx(30,getContext()));
        Id_ability_login_form_item_form_phone.setPaddingTop((int)RpxUtil.rpx(10,getContext()));
        Id_ability_login_form_item_form_phone.setWidth((int)RpxUtil.rpx(430,getContext()));
        Id_ability_login_form_item_form_phone.setPaddingBottom((int)RpxUtil.rpx(10,getContext()));
        /**
         * 验证码
          */
        //验证码

        DirectionalLayout Id_ability_login_form_item_form_auth_code_item =(DirectionalLayout) findComponentById(ResourceTable.Id_ability_login_form_item_form_auth_code_item);
        Id_ability_login_form_item_form_auth_code_item.setMarginTop((int)RpxUtil.rpx(50,getContext()));
        Text Id_ability_login_form_item_form_auth_code_label =(Text) findComponentById(ResourceTable.Id_ability_login_form_item_form_auth_code_label);
        Id_ability_login_form_item_form_auth_code_label.setTextSize((int)RpxUtil.rpx(28,getContext()));
        //输入框
        TextField Id_ability_login_form_item_form_auth_code =(TextField) findComponentById(ResourceTable.Id_ability_login_form_item_form_auth_code);
        Id_ability_login_form_item_form_auth_code.setTextSize((int)RpxUtil.rpx(28,getContext()));
        Id_ability_login_form_item_form_auth_code.setBackground(Id_ability_login_form_item_form_Element);
        Id_ability_login_form_item_form_auth_code.setMarginLeft((int)RpxUtil.rpx(30,getContext()));
        Id_ability_login_form_item_form_auth_code.setPaddingLeft((int)RpxUtil.rpx(30,getContext()));
        Id_ability_login_form_item_form_auth_code.setPaddingRight((int)RpxUtil.rpx(30,getContext()));
        Id_ability_login_form_item_form_auth_code.setPaddingTop((int)RpxUtil.rpx(10,getContext()));
        Id_ability_login_form_item_form_auth_code.setWidth((int)RpxUtil.rpx(200,getContext()));
        Id_ability_login_form_item_form_auth_code.setPaddingBottom((int)RpxUtil.rpx(10,getContext()));
        Id_ability_login_form_item_form_auth_code.setFocusChangedListener(new Component.FocusChangedListener() {
            @Override
            public void onFocusChange(Component component, boolean b) {
                if (b) {
                    // 获取到焦点
                    System.out.println("获取焦点");
                } else {
                    // 失去焦点
                    System.out.println("失去焦点");
                    String text = Id_ability_login_form_item_form_auth_code.getText();
                    System.out.println("当前获取的内容是==="+text);
                }
            }
        });
        //获取验证码
        DirectionalLayout ability_login_form_item_form_get_auth_code =(DirectionalLayout) findComponentById(ResourceTable.Id_ability_login_form_item_form_get_auth_code);
        Text auth_get_text = new Text(getContext());
        auth_get_text.setText("获取验证码");
        auth_get_text.setTextColor(new Color(MyColors.secondColor));
        auth_get_text.setTextSize((int)RpxUtil.rpx(28,getContext()));
        auth_get_text.setMarginLeft((int)RpxUtil.rpx(30,getContext()));
        auth_get_text.setPaddingLeft((int)RpxUtil.rpx(30,getContext()));
        auth_get_text.setPaddingRight((int)RpxUtil.rpx(30,getContext()));
        auth_get_text.setTextAlignment(TextAlignment.CENTER);
        auth_get_text.setPaddingTop((int)RpxUtil.rpx(10,getContext()));
        auth_get_text.setWidth((int)RpxUtil.rpx(200,getContext()));
        auth_get_text.setPaddingBottom((int)RpxUtil.rpx(10,getContext()));
        auth_get_text.setBackground(Id_ability_login_form_item_form_Element);
        ability_login_form_item_form_get_auth_code.addComponent(auth_get_text);
        ability_login_form_item_form_get_auth_code.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (timeDown==true){
                    timeDown=false;
                    //开启倒计时
                    ability_login_form_item_form_get_auth_code.removeAllComponents();
                    Text auth_text_code = new Text(getContext());
                    auth_text_code.setText(""+timenum);
                    auth_text_code.setTextColor(new Color(MyColors.secondColor));
                    auth_text_code.setTextSize((int)RpxUtil.rpx(28,getContext()));
                    auth_text_code.setTextAlignment(TextAlignment.CENTER);
                    auth_text_code.setMarginLeft((int)RpxUtil.rpx(30,getContext()));
                    auth_text_code.setPaddingLeft((int)RpxUtil.rpx(30,getContext()));
                    auth_text_code.setPaddingRight((int)RpxUtil.rpx(30,getContext()));
                    auth_text_code.setPaddingTop((int)RpxUtil.rpx(10,getContext()));
                    auth_text_code.setPaddingBottom((int)RpxUtil.rpx(10,getContext()));
                    auth_text_code.setWidth((int)RpxUtil.rpx(200,getContext()));
                    auth_text_code.setBackground(Id_ability_login_form_item_form_Element);
                    ability_login_form_item_form_get_auth_code.addComponent(auth_text_code);
                    createSerialTaskDispatcher("", TaskPriority.DEFAULT)
                            .asyncDispatch(new Runnable() {
                                @Override
                                public void run() {
                                    for (int i =timenum; i>=0 ; i--) {
                                        try {
                                            Thread.sleep(1*1000L);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                        int  index=i;
                                        getUITaskDispatcher().asyncDispatch(new Runnable() {
                                            @Override
                                            public void run() {
                                                if(index<=0){
                                                    ability_login_form_item_form_get_auth_code.removeAllComponents();
                                                    timeDown=true;
                                                    ability_login_form_item_form_get_auth_code.addComponent(auth_get_text);
                                                }
                                                auth_text_code.setText(""+index);
                                            }
                                        });
                                    }
                                }
                            });
                }else {
//                    无法点击因为当前正在倒计时

                }
            }
        });
        /**
         * 登录
         */
        DirectionalLayout Id_ability_login_form_item_form_login =(DirectionalLayout) findComponentById(ResourceTable.Id_ability_login_form_item_form_login);
        ShapeElement Id_ability_login_form_item_form_loginshapeElement = new ShapeElement();
        Id_ability_login_form_item_form_loginshapeElement.setRgbColor(MyColors.themeColorGb);
        Id_ability_login_form_item_form_loginshapeElement.setCornerRadius((int)RpxUtil.rpx(28,getContext()));
        Id_ability_login_form_item_form_login.setBackground(Id_ability_login_form_item_form_loginshapeElement);
        Id_ability_login_form_item_form_login.setMarginTop((int)RpxUtil.rpx(60,getContext()));
        Text text = new Text(getContext());
        text.setTextSize((int)RpxUtil.rpx(28,getContext()));
        text.setTextColor(Color.WHITE);
        text.setText("登录");
        text.setPaddingTop((int)RpxUtil.rpx(15,getContext()));
        text.setPaddingBottom((int)RpxUtil.rpx(15,getContext()));
        Id_ability_login_form_item_form_login.setWidth((int)RpxUtil.rpx(500,getContext()));
        Id_ability_login_form_item_form_login.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        Id_ability_login_form_item_form_login.addComponent(text);
        //登录
        Id_ability_login_form_item_form_login.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                String code = Id_ability_login_form_item_form_auth_code.getText();
                String phone = Id_ability_login_form_item_form_phone.getText();
                GoHome();
                Window window = getWindow();
            }
        });
    }
//    一键登录 登录
    private void oneKeyLogin(){
        DirectionalLayout Id_ability_login_form_item_form_phone_login =(DirectionalLayout) findComponentById(ResourceTable.Id_ability_login_form_item_form_phone_login);
        ShapeElement Id_ability_login_form_item_form_phone_loginshapeElement = new ShapeElement();
        Id_ability_login_form_item_form_phone_loginshapeElement.setRgbColor(MyColors.themeColorGb);
        Id_ability_login_form_item_form_phone_loginshapeElement.setCornerRadius((int)RpxUtil.rpx(28,getContext()));
        Id_ability_login_form_item_form_phone_login.setBackground(Id_ability_login_form_item_form_phone_loginshapeElement);
        Id_ability_login_form_item_form_phone_login.setMarginTop((int)RpxUtil.rpx(60,getContext()));
        Text text = new Text(getContext());
        text.setTextSize((int)RpxUtil.rpx(28,getContext()));
        text.setTextColor(Color.WHITE);
        text.setText("一键登录");
        text.setPaddingTop((int)RpxUtil.rpx(15,getContext()));
        text.setPaddingBottom((int)RpxUtil.rpx(15,getContext()));
        Id_ability_login_form_item_form_phone_login.setWidth((int)RpxUtil.rpx(500,getContext()));
        Id_ability_login_form_item_form_phone_login.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        Id_ability_login_form_item_form_phone_login.addComponent(text);

        Id_ability_login_form_item_form_phone_login.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                GoHome();

            }
        });
    }
    private void startTimeDown(){
        DirectionalLayout ability_login_form_item_form_get_auth_code =(DirectionalLayout) findComponentById(ResourceTable.Id_ability_login_form_item_form_get_auth_code);
        ability_login_form_item_form_get_auth_code.removeAllComponents();
        Text times = new Text(getContext());
    }
    private void GoHome(){
        Intent intent = new Intent();
//        Operation operationBuilder = new Intent.OperationBuilder()
//                .withDeviceId("")
//                .withBundleName("com.example.shangjinlieren")
//                .withAbilityName("com.example.shangjinlieren.MainAbility")
//                .build();
//        intent.setOperation(operationBuilder);
//        intent.setAction("action.list.money");
//        intent.setParam("index","1");
//        startAbility(intent);
        terminate();//销毁当前页面
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
